package com.jservice.api.service.zww;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HashKit;
import com.jservice.framework.utils.DateUtils;
import com.jservice.framework.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service(value="ssoService")
public class SSOService{


	private static String  servicecode = "wzsmkapp";
	private static String servicepwd = "wzsmkapppwd";

	public Result idValidation(String loginname, String password){
		Result result = new Result();
		String url = "http://puser.zjzwfw.gov.cn/sso/servlet/ssoauth";
		Map<String, Object> map = new HashMap<String, Object>();
		String time = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
		map.put("method", "idValidation");
		map.put("servicecode", servicecode);
		map.put("time", time);
		map.put("sign", HashKit.md5(servicecode + servicepwd + time).toLowerCase());
		map.put("loginname", loginname);
		map.put("encryptiontype", "1");
		map.put("password", password);
		map.put("datatype", "json");
		try {
			String repsponse = HttpUtils.get(url, map);
			JSONObject data = JSONObject.parseObject(repsponse);

			if("0".equals(data.getString("result"))){
				result.setResultCode(0);
				result.setResultDate(data);
			}else {
				result.setResultCode(3);
				result.setResultMsg(data.getString("errmsg"));
			}
		} catch (Exception e) {
			//0：成功，1：参数错误，2：程序异常，3：其他错误
			result.setResultCode(2);
			result.setResultMsg("用户认证异常");
			e.printStackTrace();
		}
		return result;
	}


	public Result getUserInfo(String accesstoken){
		Result result = new Result();
		String url = "http://puser.zjzwfw.gov.cn/sso/servlet/ssoauth";
		Map<String, Object> map = new HashMap<String, Object>();
		String time = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
		map.put("method", "getUserInfo");
		map.put("servicecode", servicecode);
		map.put("time", time);
		map.put("sign", HashKit.md5(servicecode + servicepwd + time).toLowerCase());
		map.put("accesstoken", accesstoken);
		map.put("datatype", "json");
		try {
            String repsponse = HttpUtils.get(url, map);
            JSONObject data = JSONObject.parseObject(repsponse);
			if("0".equals(data.getString("result"))){
				result.setResultCode(0);
				result.setResultDate(data);
			}else {
				result.setResultCode(3);
				result.setResultMsg(data.getString("errmsg"));
			}
		} catch (Exception e) {
			//0：成功，1：参数错误，2：程序异常，3：其他错误
			result.setResultCode(2);
			result.setResultMsg("根据访问令牌获取用户详细信息异常");
			e.printStackTrace();
		}
		return result;
	}

	public Result getST(String accesstoken){
		Result result = new Result();
		String url = "http://puser.zjzwfw.gov.cn/sso/servlet/ssoauth";
		Map<String, Object> map = new HashMap<String, Object>();
		String time = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
		map.put("method", "getST");
		map.put("servicecode", servicecode);
		map.put("time", time);
		map.put("sign", HashKit.md5(servicecode + servicepwd + time).toLowerCase());
		map.put("accesstoken", accesstoken);
		map.put("datatype", "json");

		try {
			String repsponse = HttpUtils.get(url, map);
			JSONObject data = JSONObject.parseObject(repsponse);
			result.setResultCode(0);
			result.setResultDate(data);
		} catch (Exception e) {
			//0：成功，1：参数错误，2：程序异常，3：其他错误
			result.setResultCode(2);
			result.setResultMsg("根据访问令牌获取票据异常");
			e.printStackTrace();
		}
		return result;
	}

	public Result getToken(String authcode) {
		Result result = new Result();
		String url = "http://puser.zjzwfw.gov.cn/sso/servlet/ssoauth";
		Map<String, Object> map = new HashMap<String, Object>();
		String time = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
		map.put("method", "getToken");
		map.put("servicecode", servicecode);
		map.put("time", time);
		map.put("sign", HashKit.md5(servicecode + servicepwd + time).toLowerCase());
		map.put("authcode", authcode);
		map.put("datatype", "json");

		try {
			String repsponse = HttpUtils.get(url, map);
			JSONObject data = JSONObject.parseObject(repsponse);

			if("0".equals(data.getString("result"))){
				result.setResultCode(0);
				result.setResultDate(data);
			}else {
				result.setResultCode(3);
				result.setResultMsg(data.getString("errmsg"));
			}
		} catch (Exception e) {
			//0：成功，1：参数错误，2：程序异常，3：其他错误
			result.setResultCode(2);
			result.setResultMsg("根据授权码获取令牌异常");
			e.printStackTrace();
		}
		return result;
	}

}
